package org.example.intern.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.intern.entity.common.SysRoster;
import org.example.intern.entity.common.SysTalent;
import org.example.intern.service.ISysRosterService;
import org.example.intern.service.ISysTalentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 人才库 前端控制器
 * </p>
 *
 * @author zhangchengming
 * @since 2022-02-21
 */
@Api(tags = "人才库控制类")
@RestController
@CrossOrigin(origins = "http://localhost:9528", allowCredentials= "true")
@RequestMapping("/talent_pool")
public class TalentController {

    @Autowired
    private ISysTalentService iSysTalentService;

    @ApiOperation("分页获取全部简历信息")
    @GetMapping("/page")
    public Map<String,Object> findRoster(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        pageNum=(pageNum-1) * pageSize;
        List<SysTalent> data = iSysTalentService.selectByPage(pageNum, pageSize);
        Integer total = iSysTalentService.selectTotal();
        Map<String,Object> res=new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }

    @ApiOperation("添加&修改人才库对象")
    @GetMapping("/save")
    public int insert_update(SysTalent sysTalent) {
        return iSysTalentService.insert_update(sysTalent);
    }



    @ApiOperation("删除人才库对象")
    @DeleteMapping("/delete")
    public int deleteTalent(Integer id) {
        return iSysTalentService.deleteTalent(id);
    }

    @ApiOperation("查找人才库对象")
    @GetMapping("/search")
    public Map<String,Object> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "5") Integer pageSize,
                                     @RequestParam(defaultValue = "") String name,
                                     @RequestParam(defaultValue = "") String phone,
                                     @RequestParam(defaultValue = "") String school,
                                     @RequestParam(defaultValue = "") String education,
                                     @RequestParam(defaultValue = "") String postion,
                                     @RequestParam(defaultValue = "") String addr,
                                     @RequestParam(defaultValue = "") String progress){
        IPage<SysTalent> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysTalent> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)){
            queryWrapper.like("name", name);
        }if (!"".equals(phone)){
            queryWrapper.like("phone", phone);
        }if (!"".equals(school)){
            queryWrapper.like("school", school);
        }if (!"".equals(education)){
            queryWrapper.like("education", education);
        }if (!"".equals(postion)){
            queryWrapper.like("postion", postion);
        }if (!"".equals(addr)){
            queryWrapper.like("addr", addr);
        }if (!"".equals(progress)){
            queryWrapper.like("progress", progress);
        }
        IPage<SysTalent> rosterPage = iSysTalentService.page(page, queryWrapper);
        List<SysTalent> data = rosterPage.getRecords();
        Long total = rosterPage.getTotal();
        Map<String,Object> res=new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }


}
